import {Component, Input, OnInit, ViewEncapsulation} from '@angular/core';
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {SharedService} from "@shared/service/shared/shared.service";

@Component({
  selector: 'seed-attention',
  templateUrl: './attention.component.html',
  styleUrls: ['./attention.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class AttentionComponent implements OnInit {
  public unfollow: string;
  public isfollow: string;
  public isfollowboth: string;

  @Input() item: any;
  @Input() i: any;
  @Input() dele: string;

  constructor(private imgService: ImageServeService,
              public toolsService: ToolsService,
              public user: AuthService,
              public sharedService: SharedService) {
    this.unfollow = this.imgService.getUnfollow();
    this.isfollow = this.imgService.getIsfollow();
    this.isfollowboth = this.imgService.getFollowboth();
  }

  ngOnInit() {

  }

  attenSubmit(itemUserid, followed, event, i?, dele?) {
    event.stopPropagation();
    const classList = event.target.classList;
    if (!classList.contains('unFollow')) {
      if (dele) {
        this.sharedService.GetFeedFriend(itemUserid, false).subscribe(res => {
          if (res.result === '0') {
            event.target.src = this.unfollow;
            classList.remove('isFollow');
            classList.remove('bothFollow');
            classList.add('unFollow');
          }
        });
      } else {
        this.toolsService.showToast('已关注~');
      }
    } else {
      if (followed === 0) {
        this.sharedService.GetFeedFriend(itemUserid).subscribe(res => {
          const obj = res;
          if (obj.result === '0') {
            if (obj.data.isFollowedMe === 0) {
              event.target.src = this.isfollow;
              classList.add('isFollow');
              classList.remove('unFollow');
            } else {
              event.target.src = this.isfollowboth;
              classList.add('bothFollow');
              classList.remove('unFollow');
            }
            if (this.user.getUserId() && i) {
              document.querySelector('#num' + i).innerHTML =
                (Number(document.querySelector('#num' + i).innerHTML) + 1).toString();
            }
          }
        });
      } else {
        this.toolsService.showToast('已关注~');
      }
    }
  }
}
